package com.android.jingweiapp.fragment.publicinfo;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.jingweiapp.R;
import com.android.jingweiapp.XMLPhase.GetGuideContentXMLPhase;
import com.android.jingweiapp.bean.GuideBean;
import com.android.jingweiapp.util.Contacts;
import com.android.jingweiapp.util.HttpUtil;
import com.google.gson.Gson;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 公开指南
 */
public class GuideFragment extends Fragment {

    @Bind(R.id.tv_detail_title)
    TextView tvDetailTitle;
    @Bind(R.id.tv_detail_time)
    TextView tvDetailTime;
    @Bind(R.id.tv_detail_from)
    TextView tvDetailFrom;
    @Bind(R.id.tv_detail_content)
    TextView tvDetailContent;

    private ProgressDialog progressDialog;

    public GuideFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_guide, container, false);
        ButterKnife.bind(this, view);

        new MyAsyncTask().execute();

        return view;
    }

    private void dealData(String str) {
        Gson gson = new Gson();
        GuideBean guideBean = gson.fromJson(str, GuideBean.class);
        GuideBean.DataEntity dataEntity = guideBean.getData().get(0);
        tvDetailTitle.setText(dataEntity.getTitle());
        tvDetailContent.setText(Html.fromHtml(dataEntity.getContent()));
        tvDetailFrom.setText(dataEntity.getSource());
        tvDetailTime.setText(dataEntity.getTime());
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    class MyAsyncTask extends AsyncTask<String, Integer, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = ProgressDialog.show(getActivity(), "加载中",
                    "请稍后", true, false);
        }

        @Override
        protected String doInBackground(String[] params) {
            return HttpUtil.postWithContent(Contacts.BASE_URL,
                    GetGuideContentXMLPhase.createParam(getActivity()),
                    new GetGuideContentXMLPhase());
        }

        @Override
        protected void onPostExecute(String str) {
            super.onPostExecute(str);
            progressDialog.dismiss();
            dealData(str);
        }
    }
}
